package com.web;

import com.net.server.NettyServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * @ClassName AppListener
 * @Description TODO
 * @Author zhuchen216628
 * @Date 2018/9/29 14:55
 * @Version 1.0
 **/
public class AppListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        System.out.println("AppListener  contextInitialized");
        WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
        if (springContext!=null){
            NettyServer nettyServer = (NettyServer) springContext.getBean("nettyServer");
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
        if (springContext!=null){
           NettyServer nettyServer = (NettyServer) springContext.getBean("nettyServer");
           nettyServer.shutdown();
        }
    }
}
